package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException 
	{
		doLogin(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException 
	{
		doLogin(req, resp);
	}
	
	private void doLogin(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException
	{
		String pageNum = req.getParameter("page");
		String userName = "";
		String passWord = "";
		if (pageNum.equals(null) || pageNum.equals("2"))
		{
			RequestDispatcher dispatch = req.getRequestDispatcher("GetLoginDetails.jsp");
			dispatch.forward(req, resp);
		}
		else if (pageNum.equals("1"))
		{
			userName = req.getParameter("userName");
			passWord = req.getParameter("passWord");
			
			req.getSession().setAttribute("userName", userName);
			req.getSession().setAttribute("passWord", passWord);
			
			RequestDispatcher dispatch = req.getRequestDispatcher("LoggedIn.jsp");
			dispatch.forward(req, resp);
		}
		
	}
}